﻿using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SleepManagement.Controllers
{
    public static class CustomLogManager
    {
        private static readonly string logFile = Path.Combine(Application.StartupPath, "logging.log");
            
        public static void LogMessage(string message)
        {
            var f = File.AppendText(logFile);
            f.Write(string.Format("{0}{1}: {2}", 
                Environment.NewLine, 
                DateTime.Now.ToString(CultureInfo.InvariantCulture),
                message));
            f.Flush();
            f.Close();
        }

        public static void LogException(Exception e)
        {
            var composeException = new StringBuilder(e.Message);
            
            var eTree = e;
            while (eTree.InnerException!=null)
            {
                eTree = eTree.InnerException;
                composeException.AppendLine().AppendLine("InnerException:").AppendLine(eTree.Message);
            }
            composeException.AppendLine().AppendLine("Stack Trace:").AppendLine(e.StackTrace).AppendLine();
            LogMessage(composeException.ToString());
        }
    }
}
